# -*- coding: utf-8 -*-
"""
Created on Fri Nov 24 10:54:42 2017

@author: Administrator
"""
# -*- coding: utf-8 -*-
# author : zf 
# shitiku.py
# 试题库测试软件
from tkinter import messagebox
#import base64
#from shifu import img
import hfut_test
import tkinter
import os
import win32api
import tkinter as tk
import time

class PopupDialog(tk.Toplevel):
  def __init__(self, parent):
      super().__init__()
      self.title('设置用户信息')
      self.geometry()
      self.parent = parent # 显式地保留父窗口
      # 第一行（两列）
      tk.Label(self, text='', width=2).grid(row=1, column=3)
      tk.Label(self, text='试题库选择', width=8).grid(row=1, sticky = 'w')
      self.LESSON = tk.StringVar()
      tk.Entry(self, textvariable=self.LESSON, width=20).grid(row=1, column=1)
      
      tk.Label(self, text='测试题数', width=8).grid(row=6, sticky = 'w')
      self.number = tk.StringVar()
      tk.Entry(self, textvariable=self.number, width=20).grid(row=6, column=1)
      
      tk.Label(self, text='登陆网址', width=8).grid(row=7, sticky = 'w')
      self.url = tk.StringVar()
      tk.Entry(self, textvariable=self.url, width=20).grid(row=7, column=1)
      
      tk.Label(self, text='睡眠时间', width=8).grid(row=8, sticky = 'w')
      self.sleeptime = tk.StringVar()
      tk.Entry(self, textvariable=self.sleeptime, width=20).grid(row=8, column=1)
      
      tk.Label(self, text='题目类型数', width=9).grid(row=9, sticky = 'w')
      self.question = tk.StringVar()
      tk.Entry(self, textvariable=self.question, width=20).grid(row=9, column=1)
      
      self.url.set(self.parent.url)
      self.number.set(self.parent.number)
      self.LESSON.set(self.parent.LESSON)
      self.sleeptime.set(self.parent.sleeptime)
      self.question.set(self.parent.question)
      
      tk.Button(self, text="取消", command=self.cancel,bg='red').grid(row=10, column=0)
      tk.Button(self, text="确定", command=self.ok,bg='green').grid(row=10, column=1)
  def ok(self):
      # 显式地更改父窗口参数
      self.parent.url = self.url.get()
     
      self.parent.LESSON = self.LESSON.get()
      self.parent.number = self.number.get()
      self.parent.sleeptime = self.sleeptime.get()
      self.parent.question = self.question.get()
      self.destroy() # 销毁窗口
  def cancel(self):
      self.destroy()
# 主窗
class MyApp(tk.Tk):
  def __init__(self):
      super().__init__()
      # self.pack() # 若继承 tk.Frame，此句必须有！！！
      self.title('试题库刷题')
    # 程序参数
      self.LESSON = 1
      self.number=105
      self.url='http://tkkc.hfut.edu.cn/'
      # 程序界面
      self.sleeptime=1
      self.question=3
      '''tmp = open("tmp.ico","wb+")
      tmp.write(base64.b64decode(img))
      tmp.close()
      self.iconbitmap("tmp.ico")
      os.remove("tmp.ico")'''
      self.setupUI()
     

      menubar = tkinter.Menu(self,bg="red")
      self.config(menu = menubar)
      operationMenu1 = tkinter.Menu(menubar, tearoff = 0) 
      menubar.add_cascade(label = "操作", menu = operationMenu1)
      operationMenu1.add_command(label = "设置区", command = self.setup_config)
      operationMenu1.add_separator()
      operationMenu1.add_command(label='退出',command=self.destroy)
      
      operationMenu2 = tk.Menu(menubar, tearoff = 0) 
      menubar.add_cascade(label = "打开", menu = operationMenu2)
      operationMenu2.add_command(label = "打开生成的答案文件", command = self.openxls)
      operationMenu2.add_separator()
      operationMenu2.add_command(label='打开试题库',command=self.opensitiku)

      fi = tk.Menu(menubar, tearoff = False) 
      menubar.add_cascade(label = "关于", menu = fi)
      fi.add_command(label='开发者：M-45',command='callback')
      fi.add_command(label='版本号：1.3.0 ',command='callback')
      fi.add_command(label='软件介绍：用来刷合肥工业大学试题库',command='callback')
      fi.add_command(label='退出',command=self.destroy)
      
  def setupUI(self):
      
      
      tk.Label(self,text='学号').grid(row=1, sticky = 'w')
      self.input11=tk.StringVar()#捕获用户输入
      self.box11=tk.Entry(self,textvariable=self.input11,width=40)#用户文本输入
      self.input11.set('2015210834')#输入框预设值
      self.box11.grid(row=1, column=1)

      tk.Label(self,text='密码').grid(row=2, sticky = 'w')
      self.input12=tk.StringVar()#捕获用户输入
      self.box12=tk.Entry(self,textvariable=self.input12,width=40)#用户文本输入
      self.input12.set('')#输入框预设值
      self.box12.grid(row=2, column=1)

      tk.Label(self,text='测试选择').grid(row=3, sticky = 'w')
      self.input13=tk.StringVar()#捕获用户输入
      self.box13=tk.Entry(self,textvariable=self.input13,width=5)#用户文本输入
      self.input13.set('1')#输入框预设值
      self.box13.grid(row=3, column=1)
      
      tk.Button(self, text="点击开始", command=self.start,bg='green').grid(row=4, column=1)
  # 设置参数
  def setup_config(self):
      pw = PopupDialog(self)
      self.wait_window(pw) # 这一句很重要！！！
      return 
  
  def openxls(self):
    if os.path.exists("试题库测试.xls"):
        win32api.ShellExecute(0,'open', "试题库测试.xls", '','',1)
    else:
        messagebox.showinfo('提示','答案文件还未生成，请进行测试后再点击')
  def opensitiku(self):
    if os.path.exists("exercise.xls"):
        win32api.ShellExecute(0,'open', "exercise.xls", '','',1)
    else:
        messagebox.showinfo('提示','文件不存在')
  def start(self):
      #messagebox.showinfo('提示','即将开启刷题，请确认已安装谷歌浏览器')
      ID=self.input11.get()
      psd=self.input12.get()
      test_choose=int(self.input13.get())
      url=self.url
      LESSON=self.LESSON
      number=int(self.number)
      question=self.question
      sleeptime=float(self.sleeptime)
      self.shuati = hfut_test.shua(ID,psd,test_choose,LESSON,number,url,sleeptime=sleeptime,question=question)
      self.shuati.login()
      messagebox.showinfo('提示','输完验证码后请点击主程序窗口的 继续按钮 继续！')
      tk.Button(self, text="继续", command=self.startnext,bg='green').grid(row=4, column=0)
  def startnext(self):
      time.sleep(1)
      self.shuati.test()
      messagebox.showinfo('提示','程序已经帮你做完，请将试题库中没有的题目自行百度做完！')
if __name__ == '__main__':
    app = MyApp()
    app.mainloop()


